import axios from 'axios'
import VueAxios from 'vue-axios'
import qs from 'qs'

export const  baseURL ="http://localhost:8080";
const service = axios.create({
  baseURL:baseURL,
  timeout: 600000 // 请求超时时间
});
// 添加request拦截器
service.interceptors.request.use(config => {
  return config
}, error => {
  Promise.reject(error)
});
// 添加respone拦截器
service.interceptors.response.use(
  response => {
    let res={};
    res.status=response.status
    res.data=response.data;
    return res;
  },
  error => {
    if(error.response && error.response.status == 404){
      router.push('/blank.vue')
    }


    return Promise.reject(error.response)
  }
);
export function get(url, params = {}) {
  params.t = new Date().getTime(); //get方法加一个时间参数,解决ie下可能缓存问题.
  return service({
    url: url,
    method: 'get',
    headers: {
    },
    params
  })
}
//封装post请求
/**
 *
 * @param url 请求相对路径
 * @param data 数据
 * @param token token 没有时传“”
 * @returns {AxiosPromise}
 */
export function post(url, data = {},token) {
  if(token.length>0){
    //默认配置
    let sendObject={
      url: url,
      method: 'post',
      headers: {
        "token":token,
        'Content-Type':'application/json;charset=UTF-8'
      },
      data:data
    };
    sendObject.data=JSON.stringify(data);
    return service(sendObject)
  }else{
    //默认配置
    let sendObject={
      url: url,
      method: 'post',
      headers: {
        'Content-Type':'application/json;charset=UTF-8'
      },
      data:data
    };
    sendObject.data=JSON.stringify(data);
    return service(sendObject)
  }
}

//封装put方法 (resfulAPI常用)
export function put(url,data = {}){
  return service({
    url: url,
    method: 'put',
    headers: {
      'Content-Type':'application/json;charset=UTF-8'
    },
    data:JSON.stringify(data)
  })
}
//删除方法(resfulAPI常用)
export function deletes(url){
  return service({
    url: url,
    method: 'delete',
    headers: {}
  })
}

//不要忘记export
export {
  service
}
